#include "CClient.h"

CClient::CClient()
{
    m_bClientstarted = false;
}

CClient::~CClient()
{
    if(m_bClientstarted == false)
            return;
}

void CClient::SetCallbackGUI(QObject* CallbackGUI)
{
    m_pConfigDialog = CallbackGUI;
    QObject::connect(this,SIGNAL(emit_UpdateChatWin(QString)),m_pConfigDialog,SLOT(receive_UpdateChatWin(QString)));
}

void CClient::SendChatMessage(const char* str)
{

}

void CClient::AttemptConnection()
{
    // initialize Zoidcom
    m_pZoidCommunicator = new ZoidCom("./logs/zoidcli.log");
    if (!m_pZoidCommunicator || !m_pZoidCommunicator->Init())
    {
        this->emitChatMessages("Failed to initialize communication object");
        return;
    }

    // make instance of our Client class
    m_pZoidClient = new CZoidClient(this);

    // this creates and initializes the network sockets
    // true = use udp socket, 0 = let OS choose UDP port, 0 = no internal socket
    bool result = m_pZoidClient->ZCom_initSockets(true, 0, 0);
    // if result is false, Zoidcom had problems while initializing
    if (!result)
    {
        this->emitChatMessages("Failed to initialize sockets");
        return;
    }

    QByteArray adressByteArray = ServerAdress.toAscii();
    const char* serverAdress = adressByteArray.data();

    ZCom_Address server_addr;
    server_addr.setAddress( eZCom_AddressUDP, 0, serverAdress);
    ZCom_ConnID connection_id = m_pZoidClient->ZCom_Connect(server_addr, NULL);

    // unable to connect
    // this happens if the connection process can't even be started
    // for some reason
    if (connection_id == ZCom_Invalid_ID)
    {
        this->emitChatMessages("Failed to establish connection");
        return;
    }

    m_bClientstarted = true;

    // Tell the GUI the server was started. This code is nested here because I have still to learn
    // about multithreading and the messagequeue in QT. I will try to emit a signal for the correct
    // GUI slot to catch and update the gui
    this->emitChatMessages("Client running");

    // register all zoidcom classes - this is needed for zoidcom. It depends on the fact that every
    // class that will be used over networks is registered as shown in the function below
    RegisterClassesForZoidOP();

    while ( m_bClientstarted )
    {
      // tell the client to process incoming data
      m_pZoidClient->ZCom_processInput();
      // tell the client to process outgoing data
      m_pZoidClient->ZCom_processOutput();
      // give up remaining cpu time
      m_pZoidCommunicator->Sleep(1);
    }

    this->emitChatMessages("Client stopped");
    return;
}

void CClient::emitChatMessages(QString message)
{
    emit_UpdateChatWin(message);
}

void CClient::start()
{
    this->AttemptConnection();
}

void CClient::RegisterClassesForZoidOP()
{
    CZoidLobbyRadio::registerClass(m_pZoidClient);
}

void CClient::requestZoidNode(ZCom_ClassID _requested_class)
{
    // did the server request a lobby radio to be created?
    // do it.
    if(_requested_class == CZoidLobbyRadio::getClassID())
    {
        m_pLobbyRadio = new CZoidLobbyRadio(m_pZoidClient);
    }
}
